 //===================1.显示表格============================== 
$(function(){
	var editRow=undefined;
	$('#grid_customer').datagrid({    
    url:'showcustomer.action',  
	 
	 method:'get',
     idField:'customerId' ,
     singleSelect:true,
     fitColumns:true,
     
      
       columns:[[
           {title:'顾客ID',
            field:'customerId',
            width:100,
          
        /**    editor:{
            	type:'validatebox',
            	//type:'celledit',
            	//options:{required:true}
            }   **/
            },
           {title:'顾客姓名',
             field:'customerName',
            width:100,
            editor:{
            	type:'validatebox',
            	options:{required:true}
            }  
            },
           {title:'顾客性别',
                field:'customerSex',
                width:100,
                editor:{
                	type:'validatebox',
                	options:{required:true}
                }  
                
            },
            {title:'顾客手机',
                    field:'customerTel',
                    width:100,
                    editor:{
                    	type:'validatebox',                   	
                    	options:{required:true}
                    }  
             },
            {title:'居住地址',
                  field:'customerAddress',
                  width:100,
                  editor:{
                	  type:'validatebox',
                  	options:{required:true}
                  }  
             },  
             {title:'顾客类别',
                 field:'customerClass',
                 width:100,
                 editor:{
                	 type:'validatebox',
                 	options:{required:true}
                 }  
             }
            ] ] ,   
    iconCls:'icon-load' ,
    pageSize:10,
    pageList:[3,6,10,15],
    border:false,
    pagination:true,
    sortOrder:'desc',
    sortName:'name',
    editable:true,
    //=========================2.1添加按钮一栏=========================================
    toolbar:[
             {text:'添加',
              iconCls:'icon-add',
              handler: function(){
            	  if(editRow!=undefined){
            		  $('#grid_customer').datagrid("endEdit",editRow);
            		  editRow=undefined;
            		}
            	  
            	  if(editRow==undefined){
            		  $('#grid_customer').datagrid('insertRow',{
   	               index: 0,	// 索引从0开始
	                 row: {
	                	// customerId;
	                		customerName:'',
	                		customerSex:'',
	                		
	                		customerTel:'',
	                		customerAddress:'',
	                		customerClass:''
	                		} 
	                		
                  });
                       $('#grid_customer').datagrid('beginEdit',0);
                              editRow=0;}
            	  
              
                
              
              }
             },'-',
//=========================2.2删除按钮=================================
             {text:'删除111',
              iconCls:'icon-remove',
 //===================================================================            
            handler: function(){
            	  
            	  remove();
            	  
              },
 
             },'-',
             {text:'编辑',
             
              iconCls:'icon-edit',
//===============================2.3编辑按钮======================================================
              handler: function(){
            	  var rows;
            	  rows=$('#grid_customer').datagrid('getSelections');
            	 if(rows.length!=1){
            		
            		 $.messager.alert('提示','魏沁,你还没有选择行','error');
            	 };
            	 if(rows.length==1){
            		 if(editRow!=undefined){
            		 $('#grid_customer').datagrid('endEdit',editRow);
            		editRow=undefined;
            	};
            		 if(editRow==undefined){
            			 var index=$('#grid_customer').datagrid('getRowIndex',rows[0]);
                		 $('#grid_customer').datagrid('beginEdit',index);
                		 editRow=index};
            	 }
              },
             },'-',
 //======================================2.4保存按钮=============================================
             {text:'保存',
                 iconCls:'icon-remove',
                 handler: function(){
               $('#grid_customer').datagrid('endEdit', editRow);
                 editRow=undefined;
                $('#grid_customer').datagrid('unselectAll');//我自己添加的
                 }
                },'-',
//======================================2.4保存按钮=============================================
             {text:'取消',
                 iconCls:'icon-remove',
                 handler: function(){
              
                 editRow=undefined;
                 $('#grid_customer').datagrid('rejectChanges');
                 $('#grid_customer').datagrid('unselectAll');
                 
                 }
                }
            ],
//===============================3.数据提交===========================
            onAfterEdit:function(rowIndex,rowData,changes){
            	var inserted=$('#grid_customer').datagrid("getChanges","inserted");
            	var updated=$('#grid_customer').datagrid("getChanges","updated");
            	var url='';
            	if(inserted.length>0){
            		$.post('addcustomer.action', 
            				rowData,
                             function (result) { 
                                if (result.success) {
                                	
                                   $('#grid_customer').datagrid('load');
                                    $.messager.show({   //显示正确信息
                                        title: '提示',
                                        msg: result.msg
                                    });                                 
                                } else {
                                    $.messager.show({   //显示错误信息
                                        title: '错误',
                                        msg: result.msg
                                    });
                                }
                            }, 'json');
            	}
                  if(updated.length>0){
                	  $.post('editcustomer.action', 
                              rowData,
                               function (result) { 
                                  if (result.success) {
                                 
                                     $('#grid_customer').datagrid('load');
                                      $.messager.show({   //显示正确信息
                                          title: '提示',
                                          msg: result.msg
                                      });                                 
                                  } else {
                                      $.messager.show({   //显示错误信息
                                          title: '错误',
                                          msg: result.msg
                                      });
                                  }
                              }, 'json');
            	}
                  
            	
            	
            	editRow=undefined;
            },
  
            
          
})




});


//===============================6编辑方法===================================================/* 删除数据*/
function remove(){
    var row = $('#grid_customer').datagrid('getSelected');
    //如果没有选中记录
    if(row == null){
        $.messager.alert("提示", "请选择一条记录",'info');
        return;
    }
    $.messager.confirm('确认', '确定要删除吗？', function (r) {
            if (r) {
                //提交到后台的action
                $.post('delcustomer.action', 
                { customerId: row.customerId },
                 function (result) { 
                    if (result.success) {
                    	
                    	
                        $.messager.show({   //显示正确信息
                            title: '提示',
                            msg: result.msg
                        });      
                        $('#grid_customer').datagrid('reload');
                    } else {
                        $.messager.show({   //显示错误信息
                            title: '错误',
                            msg: result.msg
                        });
                    }
                }, 'json');
            }
        });
     
}
//======================9.查询功能=========================================================	


beginsearch=function(){
	$('#grid_customer').datagrid('load',{
		customerId:$('#form_customer').find('[name=customerId]').val(),
		customerName:$('#form_customer').find('[name=customerName]').val(),
		customerSex:$('#form_customer').find('[name=customerSex]').val(),
		customerTel:$('#form_customer').find('[name=customerTel]').val(),
		customerClass:$('#form_customer').find('[name=customerClass]').val(),});

	
};
//======================10.清空查询========================================================

clearsearch=function(){
	$('#form_customer').find('input').val('');
	
	$('#grid_customer').datagrid('load',{});

	
};


